package com.mcml.space;

import org.bukkit.Effect;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.ItemStack;

public class DamageResulter implements Listener {
    @EventHandler
    public void DamageResulter(EntityDamageByEntityEvent event) {
        Entity tempattacker = event.getDamager();
        Entity tempbeenattacker = event.getEntity();
        if ((tempattacker instanceof LivingEntity) && (tempbeenattacker instanceof LivingEntity)) {
            LivingEntity attacker = (LivingEntity) tempattacker;
            LivingEntity entity = (LivingEntity) tempbeenattacker;
            ItemStack attackitem = null;
            ItemStack entityitem = null;
            EntityEquipment entityeq = entity.getEquipment();
            EntityEquipment attackereq = attacker.getEquipment();
            if (attacker instanceof Player) {
                attackitem = ((Player) attacker).getItemInHand();
            }
            if (entity instanceof Player) {
                entityitem = ((Player) entity).getItemInHand();
            }
            if (attackitem == null || ItemLores.isMotaRPGItem(attackitem)) {
                ItemStack[] ac;
                int acl;
                int i;
                int physicsdamage;
                int physicsAttack = 0;
                int physicsProtect = 0;
                int magicDamage = 0;
                int magicProtect = 0;
                int combieTimes = 0;
                int critChance = 0;
                boolean crit = false;
                boolean strong = false;
                if (entityitem != null) {
                    physicsProtect = ItemLores.getPhysicsProtect(entityitem);
                    magicProtect = ItemLores.getMagicProtect(entityitem);
                    strong = ItemLores.IsStrong(entityitem);

                }
                if (entityeq != null) {
                    ac = entityeq.getArmorContents();
                    acl = ac.length;
                    for (i = 0; acl < i; i++) {
                        ItemStack thisitem = ac[i];
                        if (thisitem != null) {
                            magicProtect += ItemLores.getMagicProtect(thisitem);
                            physicsProtect += ItemLores.getPhysicsProtect(thisitem);
                            if (!strong) {
                                strong = ItemLores.IsStrong(entityitem);
                            }
                        }
                    }
                }
                if (attackitem != null) {
                    if (!strong) {
                        physicsAttack = ItemLores.getPhysicsAttack(attackitem);
                    } else {
                        physicsAttack = 1;
                    }
                    magicDamage = ItemLores.getMagicAttack(attackitem);
                    combieTimes = ItemLores.getCombieTimes(attackitem);
                    critChance = ItemLores.getCritChance(attackitem);
                }
                if (attackereq != null) {
                    ac = entityeq.getArmorContents();
                    acl = ac.length;
                    for (i = 0; acl > i; i++) {
                        if (ac[i] != null) {
                            if (!strong) {
                                physicsAttack += ItemLores.getPhysicsAttack(ac[i]);
                            } else {
                                physicsAttack += 1;
                            }
                            magicDamage += ItemLores.getMagicAttack(ac[i]);
                        }
                    }
                }
                if (Utils.randomNumber(100) < critChance) {
                    crit = true;
                }

                if (physicsAttack <= physicsProtect) {
                    physicsdamage = 0;
                } else {
                    physicsdamage = physicsAttack - physicsProtect;
                }
                int enddamage = (physicsdamage + magicDamage) * combieTimes;
                int endmagicProtect = magicProtect * combieTimes;
                if (crit) {
                    enddamage *= 2;
                    attacker.getWorld().playEffect(entity.getEyeLocation(), Effect.STEP_SOUND, Material.REDSTONE_BLOCK);
                }
                Utils.Heal(entity, (double) endmagicProtect);
                if (attackitem != null) {
                    entity.damage((double) enddamage);
                    event.setDamage(0.0d);
                } else if (strong) {
                    event.setDamage(1.0d);
                } else {
                    event.setDamage(event.getDamage() - ((double) physicsProtect));
                }
            }
        }

    }
}